package com.itheima.tcp.socketFile1;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.UUID;
import java.util.concurrent.*;

public class ServiceFile {
    public static void main(String[] args) throws IOException {
        ServerSocket serverSocket = new ServerSocket(10000);
        Socket socket = null;

        ThreadPoolExecutor poor = new ThreadPoolExecutor(
                5,  //核心线程数
                10,//总线程
                10,
                TimeUnit.SECONDS,
                new ArrayBlockingQueue<>(10),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
        while (true) {
            socket = serverSocket.accept();
            FileUpload upload = new FileUpload(socket);
            //new Thread(upload) .start();

            //
            /*try {
                BufferedInputStream bis = new BufferedInputStream(socket.getInputStream());
                // BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(file));
//            BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16_网络编程\\ServiceDir\\"+ System.currentTimeMillis() + ".jpg"));
                BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("day16_网络编程\\ServiceDir\\"+ UUID.randomUUID().toString() + ".jpg"));
                int b;
                while ((b = bis.read()) != -1){
                    bos.write(b);
                }
                BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
                bw.write("上传成功");
                bw.newLine();

                bw.flush();
                bos.close();
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }*/
            poor.submit(upload);
        }
       //
    }
}
